import { Component, OnInit } from '@angular/core';
import { Location } from "@angular/common";
import { FormBuilder, FormGroup, Validators } from '../../../../../node_modules/@angular/forms';
import { ActivatedRoute, ParamMap } from '../../../../../node_modules/@angular/router';
import { HttpService } from '../../..';
import { switchMap } from '../../../../../node_modules/rxjs/operators';
import { NzModalService, NzMessageService } from '../../../../../node_modules/ng-zorro-antd';

/**
 * ### 班级演练分数设置内容
 */
@Component({
  selector: 'app-class-score-set',
  templateUrl: './class-score-set.component.html',
  styleUrls: ['./class-score-set.component.scss']
})
export class ClassScoreSetComponent implements OnInit {
  dataSet = [];
  validateForm: FormGroup;
  selectedId: number;
  isloading = false;
  get entityValue() {
    return {
      groupScore: this.validateForm.controls['groupScore'].value,
      groupMaxTime: this.validateForm.controls['groupMaxTime'].value,
      teacherGroupScore: this.validateForm.controls['teacherGroupScore'].value,
      teacherGroupTime: this.validateForm.controls['teacherGroupTime'].value,
      freeScore: this.validateForm.controls['freeScore'].value,
      freeMaxTime: this.validateForm.controls['freeMaxTime'].value,
      roleScore: this.validateForm.controls['roleScore'].value,
      roleMaxTime: this.validateForm.controls['roleMaxTime'].value,
      drillPoint: this.validateForm.controls['drillPoint'].value,
      reportPoint: this.validateForm.controls['reportPoint'].value,
      Classid: this.validateForm.controls['classInfoId'].value,
      DrillPercent: this.validateForm.controls['drillPercent'].value,
      ReportPercent: this.validateForm.controls['reportPercent'].value,
      TotalPoint: this.validateForm.controls['totalPoint'].value,
      projectId: this.selectedId
    };
  }
  get drillScore() {
    return this.entityValue.groupScore * this.entityValue.groupMaxTime + this.entityValue.teacherGroupScore * this.entityValue.teacherGroupTime +
      this.entityValue.freeScore * this.entityValue.freeMaxTime + this.entityValue.roleScore * this.entityValue.roleMaxTime;
  }

  get reportPercent() {
    return 100 - this.entityValue.DrillPercent;
  }

  constructor(
    private fb: FormBuilder,
    private route: ActivatedRoute,
    private http: HttpService,
    private location: Location,
    private _message: NzMessageService,
  ) {
    this.validateForm = this.fb.group({
      groupScore: [0, [Validators.required]],
      groupMaxTime: [0, [Validators.required]],
      teacherGroupScore: [0, [Validators.required]],
      teacherGroupTime: [0, [Validators.required]],
      freeScore: [0, [Validators.required]],
      freeMaxTime: [0, [Validators.required]],
      roleScore: [0, [Validators.required]],
      roleMaxTime: [0, [Validators.required]],
      drillPoint: [0, [Validators.required]],
      reportPoint: [0, [Validators.required]],
      classInfoId: [null, [Validators.required]],
      drillPercent: [0, [Validators.required]],
      reportPercent: [0, [Validators.required]],
      totalPoint: [0, [Validators.required]]
    });
  }

  ngOnInit() {
    this.isloading = true;
    this.route.paramMap.pipe(
      switchMap((params: ParamMap) => {
        this.selectedId = +params.get('id');
        return this.getClasses();
      })
    ).subscribe(e => { this.isloading = false; });
  }

  resetForm(e: MouseEvent) {
    e.preventDefault();
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      if (this.validateForm.controls[key]) {
        this.validateForm.controls[key].markAsPristine();
        this.validateForm.controls[key].updateValueAndValidity();
      }
    }
    this.location.back();
  }

  submitForm = async ($event) => {
    $event.preventDefault();
    for (const key in this.validateForm.controls) {
      if (this.validateForm.controls[key]) {
        this.validateForm.controls[key].markAsDirty();
        this.validateForm.controls[key].updateValueAndValidity();
      }
    }
    if (!this.validateForm.valid) {
      return;
    }
    if (this.entityValue.DrillPercent > 100 || this.entityValue.DrillPercent < 0) {
      this._message.error('演练比例高于100%');
      return;
    }
    const postBody = { ...this.entityValue };
    postBody.ReportPercent = 100 - this.entityValue.DrillPercent;
    postBody.drillPoint = this.drillScore;
    this.isloading = true;
    await this.http.post<any>('TeacherInfo/CreateOrUpdateScoreSet', postBody);
    this._message.success("保存成功！");
    this.isloading = false;
  }

  /**
   * 获取班级
   */
  async getClasses() {
    this.dataSet = await this.http.get<any>(`TeacherInfo/GetTeacherProjectClasses?projectId=${this.selectedId}`);
  }

  async getClassSet(classid: any) {
    if (classid) {
      this.isloading = true;

      const reslut = await this.http.get<any>(`TeacherInfo/GetTeacherProjectClassScoreSet?classid=${classid}&projectId=${this.selectedId}`);
      this.validateForm.controls['groupScore'].setValue(reslut['groupScore']);
      this.validateForm.controls['groupMaxTime'].setValue(reslut['groupMaxTime']);
      this.validateForm.controls['teacherGroupScore'].setValue(reslut['teacherGroupScore']);
      this.validateForm.controls['teacherGroupTime'].setValue(reslut['teacherGroupTime']);
      this.validateForm.controls['freeScore'].setValue(reslut['freeScore']);
      this.validateForm.controls['freeMaxTime'].setValue(reslut['freeMaxTime']);
      this.validateForm.controls['roleScore'].setValue(reslut['roleScore']);
      this.validateForm.controls['roleMaxTime'].setValue(reslut['roleMaxTime']);
      this.validateForm.controls['drillPoint'].setValue(reslut['drillPoint']);
      this.validateForm.controls['reportPoint'].setValue(reslut['reportPoint']);
      this.validateForm.controls['drillPercent'].setValue(reslut['drillPercent']);
      this.validateForm.controls['reportPercent'].setValue(reslut['reportPercent']);
      this.validateForm.controls['totalPoint'].setValue(reslut['totalPoint']);
      this.isloading = false;
    }

  }
}
